class CommunicationsController < ApplicationController
	
	
	def index
        if authorize
    		@users = getUsers
            if session[:loginrole] == "Admin"
                @dashboard = "Go to Gradebook"
            end
        end
		if(session[:message].nil?)
			@email_message = ""
		else
			@email_message = session[:message]
		end
		if(session[:subject].nil?)
			@subject = ""
		else
			@subject = session[:subject]
		end
	end
	
	
	private
	def getUsers
		value = "<option>-Select User-</option>"
		if(session[:recipient] == "All Users")
			value += "<option selected=\"selected\">All Users</option>"
		else
			value += "<option>All Users</option>"
		end
		people = Person.find(:all, :order => "firstname, surname")
		people.each do |p|
			if(session[:recipient] == "#{p[:firstname]} #{p[:surname]}")
				value += "<option selected=\"selected\">#{p[:firstname]} #{p[:surname]}</option>"
			else
				value += "<option>#{p[:firstname]} #{p[:surname]}</option>"
			end
		end
		value
	end
	

    def authorize 
        unless session[:loggedin] == true
            flash[:notice] = "Please log in" 
            redirect_to :controller => :login, :action => :index
        end
        return true 
    end 


	# Sends a message to the user that was selected in communications page
	# if they selected all users then all users emails are added to the list
	# of recipients
	public
	def sendMessage
		to_addrs = []
		
		if(params[:recipient] == '-Select User-')
			session[:recipient] = params[:recipient]
			session[:subject] = params[:subject]
			session[:message] = params[:message]
			flash[:notice] = "No user selected to send email to"
			redirect_to :controller => :communications, :action => :index
#			render :action => :index
			return
		end
		
		if(params[:subject] == '')
			session[:recipient] = params[:recipient]
			session[:subject] = params[:subject]
			session[:message] = params[:message]
			flash[:notice] = "Subject line is empty"
			redirect_to :controller => :communications, :action => :index
#			render :action => :index
			return
		end
		
		if(params[:message] == '')
			session[:recipient] = params[:recipient]
			session[:subject] = params[:subject]
			session[:message] = params[:message]
			flash[:notice] = "Message is empty"
			redirect_to :controller => :communications, :action => :index
#			render :action => :index
			return
		end
		
		if(params[:recipient] == 'All Users')
			people = Person.find(:all)
			people.each do |p|
				contact = Contact.find(:first, :conditions => ["id = ?", p.id])
				to_addrs << contact.email
			end
		else
			name = params[:recipient].split
			person = Person.find(:first, :conditions => ["firstname = ? AND surname = ?", name[0], name[1]])
			contact = Contact.find(:first, :conditions => ["id = ?", person.id])
			to_addrs << contact.email
		end
		from_user = Contact.find(:first, :conditions => ["id = ?", session[:loginuid]])
		MessageMailer.deliver_send(to_addrs, from_user, params[:subject], params[:message])
#		email = MessageMailer.create_send(to_addrs, from_user, params[:subject], params[:message])
#		render(:text => "<pre>" + email.encoded + "</pre>")
		session[:subject] = nil
		session[:message] = nil
		session[:recipient] = ""
		flash[:notice] = "Message Sent"
		redirect_to :controller => :communications, :action => :index
#			render :action => :index
	end
	
	
end
